#include "trace_pool.h"
#include "trace.h"
#include "iltdrawprim.h"
#include "..\clientinterfaces.h"
#include "iltclient.h"
#include "..\Screen\tex_mgr.h"

trace_pool::trace_pool(void):
m_max_nums(1000),
m_tex(0)
{
	LT_POLYGT4 temp;
	for (int i=0;i<4;++i)
	{
		temp.verts[i].rgba.r=temp.verts[i].rgba.g=temp.verts[i].rgba.b=0;
	}
	
	m_trace_pool.reserve(m_max_nums+5);
	m_trace_polys.reserve(m_max_nums+5);

	for (int i=0;i<m_max_nums;++i)
	{
		m_trace_pool.push_back(0);
		m_trace_polys.push_back(temp);
	}
	m_curretn_pos=0;
	

}
trace_pool::~trace_pool(void)
{
	for (m_trace_pool_it=m_trace_pool.begin();m_trace_pool_it!=m_trace_pool.end();++m_trace_pool_it)
	{
		if (*m_trace_pool_it)
		{
			delete (*m_trace_pool_it);
			(*m_trace_pool_it)=0;
		}
	}
	m_trace_pool.clear();
	m_trace_polys.clear();
	

}

void trace_pool::push( LTVector p1,LTVector p2,LTVector p3,LTVector p4,float power )
{
	if (m_curretn_pos>=m_max_nums)
	{
		m_curretn_pos=0;
	}
	if (m_trace_pool[m_curretn_pos])
	{
		m_trace_pool[m_curretn_pos]->revalue(p1,p2,p3,p4,power);
		m_trace_polys[m_curretn_pos]=m_trace_pool[m_curretn_pos]->m_poly;
	}
	else
	{
		trace* m_temp=new trace;
		for (int i=0;i<4;++i)
		{
			m_temp->m_poly.verts[i].rgba.r=m_temp->m_poly.verts[i].rgba.g=m_temp->m_poly.verts[i].rgba.b=0;
		}


		m_temp->revalue(p1,p2,p3,p4,power);
		m_trace_pool[m_curretn_pos]=m_temp;
		m_trace_polys[m_curretn_pos]=m_temp->m_poly;

	}

	++m_curretn_pos;
	//g_pLTClient->CPrint("%d",m_curretn_pos);



}

void trace_pool::render()
{
    
	g_pLTCDrawPrim->SetTransformType(DRAWPRIM_TRANSFORM_WORLD);
	g_pLTCDrawPrim->SetZBufferMode(DRAWPRIM_ZRW);
	g_pLTCDrawPrim->SetColorOp(DRAWPRIM_MODULATE);
	g_pLTCDrawPrim->BeginDrawPrim();
	//for (m_trace_pool_it=m_trace_pool.begin();m_trace_pool_it!=m_trace_pool
	//	.end();++m_trace_pool_it)
	//{
	//	if (*m_trace_pool_it)
	//	{
	//		(*m_trace_pool_it)->render();
	//	}
	//}
	if (!m_tex)
	{
		m_tex=tex_mgr::single().get_tex("trace");
	}
	g_pLTCDrawPrim->SetTexture(m_tex);
	static LT_POLYGT4* temp=&m_trace_polys.front();
	g_pLTCDrawPrim->DrawPrim(temp,m_max_nums);
	g_pLTCDrawPrim->EndDrawPrim();
	

}
